package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.DIYException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-15
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    //注入服务调用
    @Autowired
    private VodClient vodClient;
    @Autowired
    private EduVideoService videoService;

    //add
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    //delete
    //TODO  需要完善，删除小节的时候，同时删掉视频
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        EduVideo video=videoService.getById(id);
        //远程调用
        //先删除视频 videoSourceId
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            R result=vodClient.removeVideo(videoSourceId);
            if (result.getCode()==20001){
                throw new DIYException(20001,"熔断器");
            }
        }
        videoService.removeById(id);
        return R.ok();
    }
    //update updateVideo


}

